package com.njtoyo.taxi.admin.service.business.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.library.common.util.CopyUtil;
import com.njtoyo.taxi.admin.mapper.master.entity.DeviceReport;
import com.njtoyo.taxi.admin.mapper.master.mapper.DeviceReportMapper;
import com.njtoyo.taxi.admin.rest.pojo.dto.DeviceReportParamDto;
import com.njtoyo.taxi.admin.rest.presenter.business.DeviceReportPresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.device.DeviceReportQueryWrapper;
import com.njtoyo.taxi.admin.service.business.DeviceReportService;
import com.taxi.entity.common.RestResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class DeviceReportServiceImpl implements DeviceReportService {

    @Resource
    private DeviceReportMapper deviceReportMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult<Boolean> reportDevice(DeviceReportParamDto dto) {
        String deviceId = dto.getDeviceId();
        LambdaQueryWrapper<DeviceReport> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DeviceReport::getDeviceId, deviceId);
        DeviceReport deviceReport = deviceReportMapper.selectOne(queryWrapper);
        if(ObjectUtils.isEmpty(deviceReport)){
            DeviceReport copyResult = CopyUtil.copy(dto, DeviceReport.class);
            deviceReportMapper.insert(copyResult);
        }else{
            deviceReportMapper.updateDeviceReport(dto);
        }
        return RestResult.success();
    }

    @Override
    public RestResult<Page<DeviceReportPresenter>> queryList(DeviceReportQueryWrapper queryWrapper) {
        Page<DeviceReportPresenter> pageList = deviceReportMapper.queryList(queryWrapper);
        return RestResult.success(pageList);
    }
}
